﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class FeiLiao : Form
    {
        Dictionary<string, string> data;
        cMethods.DBHelper db;
        DataTable dt ;
        public FeiLiao(Dictionary<string,string> par)
        {
            InitializeComponent();
            db = new DBHelper();
            data = par;
        }

        private void FeiLiao_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "废料录入";
                dtpDate.CustomFormat = "yyyy-MM-dd"; //使用自定义格式
                dtpDate.Format = DateTimePickerFormat.Custom;
                lbCode.Text = data["barCode"];
                loadData();
                Addtypes();

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        DataTable flTable;
        /// <summary>
        /// 废料类型
        /// </summary>
        private void Addtypes() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select cName,cCode from fun_CommonData_Info where ");
            sb.Append("status='Y' and isDel='0' and cType='cmData'");
            flTable = db.getData(sb.ToString(), "");       
            for (int i = 0; i < dvFl.Rows.Count; i++)
            {
                for (int j = 0; j < flTable.Rows.Count; j++)
                {
                    ((DataGridViewComboBoxCell)dvFl.Rows[i].Cells["wType"]).Items.Add(flTable.Rows[j]["cName"].ToString());
                }
            }
        }
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                List<string> sqlList = new List<string>();
                StringBuilder sb;
                for (int i = 0; i < dvFl.Rows.Count; i++)
                {
                    
                    if (dvFl.Rows[i].Cells["number"].Value.ToString() != "" && dvFl.Rows[i].Cells["wType"].Value.ToString() != "")
                    {
                        string barCode = dvFl.Rows[i].Cells["barCode"].Value.ToString();
                        string num = dvFl.Rows[i].Cells["number"].Value.ToString();
                        string types = dvFl.Rows[i].Cells["types"].Value.ToString();
                        string wTypeName = dvFl.Rows[i].Cells["wType"].Value.ToString();
                        string memo = dvFl.Rows[i].Cells["memo"].Value.ToString();
                        var dr = flTable.Select("cName='" + wTypeName + "'");
                        string wType = "";
                        if (dr.Length > 0)
                        {
                            wType = dr[0]["cCode"].ToString();
                        }
                        sb = new StringBuilder();
                        sb.Append("insert into pi_ProductionPlan_waste ");
                        sb.Append("(gxCode,number,unit,createuser,createtime,wType,isDel,happenDate,barCode,memo)values");
                        sb.Append("('" + dvFl.Rows[i].Cells["types"].Value + "','" + num + "','Kg','"+ CommonMethods.userCode +"',");
                        sb.Append("'" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + wType + "',");
                        sb.Append("'0','" + dtpDate.Text + "','" + barCode + "','"+memo+"')");
                        sqlList.Add(sb.ToString());
                    }
                }
                if (sqlList.Count <1)
                {
                    MessageBox.Show("没有数据", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                var jg = db.TransUpdate(sqlList);
                if (jg > 0)
                {
                    MessageBox.Show("数据添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void loadData() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select barCode,pusers,types,typesName,'' ttypes from V_BarCode_Info ");
            sb.Append("");
            sb.Append(" where barCode='" + data["barCode"] + "' or relationBar='" + data["barCode"] + "'");
   
            var table= db.getData(sb.ToString(), "");
            table = db.getData(sb.ToString(), "");
            this.dvFl.AutoGenerateColumns = false;
            this.dvFl.DataSource = table;
      
        }


    }
}
